package com.bnz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bbbbbbbbbbbb
 * @since 2022-10-21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClassesService classesService;


    @Override
    public List<Student> findAll() {

        return this.list();
    }

    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {


        IPage<Student> queryWrapper=new Page(page,pageSize);

        IPage<Student> studentIPage=this.page(queryWrapper,null);

        for (Student record : studentIPage.getRecords()) {
            Classes classes=classesService.getById(record.getCid());
            record.setCname(classes.getCname());
        }


        return new PageResult(studentIPage.getTotal(),studentIPage.getRecords());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, Student student) {


        IPage<Student> idea =new Page<>(page,pageSize);
        QueryWrapper<Student> query=new QueryWrapper<>();
        if(student!=null){
            if(StringUtils.isNotBlank(student.getSname())){
                query.like("sname",student.getSname());
            }
            if(StringUtils.isNotBlank(student.getAddr())){
                query.like("addr",student.getAddr());
            }
            if(student.getCid() != null && student.getCid() != 0){
                query.eq("cid",student.getCid());
            }

        }

        IPage<Student> studentIPage=this.page(idea,query);

        for (Student record : studentIPage.getRecords()) {
            Classes classes=classesService.getById(record.getCid());
            record.setCname(classes.getCname());
        }

        return new PageResult<>(studentIPage.getTotal(),studentIPage.getRecords());
    }

    @Override
    public void add(Student student) {
        this.save(student);
    }

    @Override
    public void update(Student student) {
        this.updateById(student);
    }

    @Override
    public void delete(String[] sids) {
        this.removeByIds(Arrays.asList(sids));
    }

}
